/*
 * p4745.cpp
 *
 *  Created on: 2013-10-3
 *      Author: zy
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int dp[2010][2010];
int n, a[2010], ans;
int main()
{
	while (scanf("%d", &n) && n)
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			a[i + n] = a[i];
		}
		ans = 0;
		memset(dp, 0, sizeof(dp));
		for (int i = 0; i <= 2 * n; i++)
			dp[i][i] = 1;
		for (int i = 2 * n - 1; i > 0; i--)
			for (int j = i + 1; j <= 2 * n; j++)
			{
				dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
				if (a[i] == a[j])
					dp[i][j] = max(dp[i][j], dp[i + 1][j - 1] + 2);
			}
		for (int i = 1; i <= n; i++)
			ans = max(ans, dp[i][i + n - 1]);
		for (int i = 1; i <= n; i++)
			ans = max(ans, dp[i][i + n - 2] + 1);
		printf("%d\n", ans);
	}
	return 0;
}
